var nc:NetConnection;

function getConnection():NetConnection
{
	if (nc==null)
	{
		var gatewayUri:String;
		gatewayUri=Packages.system.mlf.GateKeeper.accessSystemConfig("remotingGateway");
		nc=new NetConnection();
		nc.onStatus=function(infoObj)
		{
			 Packages.log("RemotingCall.nc", "debug");
			 Packages.log("Level: "+infoObj.level, "debug");
			 Packages.log("Code: "+infoObj.code, "debug");
		}
		nc.connect(gatewayUri);
	}
	return nc;
}

function execute(serviceObj:Object, responser, index:Number)
{
	Packages.log(this._name+".execute");

	for (var a in serviceObj.params)
	{
		Packages.log(a+": "+serviceObj.params[a], "debug");
	}
	
	// create callback object
	var remotingCall:Object=new Object();
	remotingCall.onResult=onResult;
	remotingCall.onStatus=onStatus;

	remotingCall.responser=responser;
	remotingCall.index=index;

	if (serviceObj.params!=null)
	{
		this.getConnection().call(serviceObj.method, remotingCall, serviceObj.params);
		Packages.log("remote method: "+serviceObj.method);
	}
}

function onResult(result)
{
	Packages.log("onResult: "+result.returnCode); /*returnCode, returnObj*/
	//for (var a in result) Packages.log("onRult.."+a+"="+result[a]);

	var rst:Object=new Object();
	rst.index=this.index;
	rst.data=result;

	this.responser.handleRequestResult(rst);
}

function onStatus(status)
{
	Packages.log("onStatus: "+status);
}

function rtmpCaller(item:Object)
{
	var nc=getConnection();
	
	item.param.sessionId=Packages.model.Context.sessionID;
	
	if (item.type!=null) 
	{
		nc.call(item.type, item.result, item.param);
	}
	Packages.log("rtmpCaller method: "+item.type);
}